<?php
function dbUpdate($post_info, $mysqli) {
    $qry_str = "SELECT SID FROM subscribed WHERE sname = '" . $post_info["blog"] . "'";

    // Query SID.
    $result = mysqli_query($mysqli, $qry_str);
    if (!mysqli_num_rows($result)) {
      echo "nothing";
    } else {
      while ($row = mysqli_fetch_array($result)) {
        $sid = $row['SID'];
      }
    }
    mysqli_free_result($result);

    
    // If the post hasn't been tracked before, add the post into the info table. Otherwise, update last_count and last_track.
    $qry_str = "SELECT COUNT(*) AS temp_post_count FROM info WHERE PID = " . $post_info["id"] . "";
    $result = $mysqli->query($qry_str);
    if (!mysqli_num_rows($result)) {
    } else {
      $row = mysqli_fetch_array($result);
      if ($row["temp_post_count"] == 0) {
        $qry_str = "INSERT INTO info VALUES (" . $post_info["id"] . ",'" . $post_info["url"] . "','" . $post_info["image"] . "','" . $post_info["date"] . "','" . $post_info["last_track"] . "'," . $post_info["last_count"] . ")";
        insert_update($mysqli, $qry_str);
        
        $id = mysqli_query($mysqli, "SELECT COUNT(*) AS temp_id FROM text");
        if (!mysqli_num_rows($id)) {
        } else {
          $row = mysqli_fetch_array($id);
          $id = $row["temp_id"];
        }

        
        // If the text is no longer than 2048, insert directly into the text table. Otherwise, parse them into chunks of 2048 characters and insert into the text table multiple times.
        if (strlen($post_info["text"]) <= 2048) {
          $qry_str = "INSERT INTO text VALUES(" . $id . "," . $post_info["id"] . ",'" . $post_info["text"] . "')";
          insert_update($mysqli, $qry_str);
          $id += 1;
        } else {
          while (strlen($post_info["text"]) > 2048) {
            $text_chunk = substr($post_info["text"], 0, 2047);
            $post_info["text"] = substr($post_info["text"], 2047);
            $qry_str = "INSERT INTO text VALUES(" . $id . "," . $post_info["id"] . ",'" . $text_chunk . "')";
            insert_update($mysqli, $qry_str);
            $id += 1;
          }
          $qry_str = "INSERT INTO text VALUES(" . $id . "," . $post_info["id"] . ",'" . $post_info["text"] . "')";
          insert_update($mysqli, $qry_str);
          $id += 1;
        }
      } else {
        $qry_str = "UPDATE info SET last_track = '" . $post_info["last_track"] . "', last_count = " . $post_info["last_count"] . " WHERE PID = " . $post_info["id"];
        insert_update($mysqli, $qry_str);
      }
    }
    
    // Query other important tracking information about the post.
    $tid = mysqli_query($mysqli, "SELECT COUNT(*) AS temp_tid FROM tracking");
    if (!mysqli_num_rows($tid)) {
    } else {
      $row = mysqli_fetch_array($tid);
      $tid = $row["temp_tid"];
    }
    $track_count = mysqli_query($mysqli, "SELECT COUNT(*) AS temp_track_count FROM relation WHERE PID = " . $post_info["id"]);
    if (!mysqli_num_rows($track_count)) {
    } else {
      $row = mysqli_fetch_array($track_count);
      $track_count = $row["temp_track_count"];
    }
    
    $qry_str = "SELECT cnt FROM tracking T, relation R WHERE T.TID = R.TID AND PID = " . $post_info["id"] . " ORDER BY cnt DESC LIMIT 1";
    $last_count = mysqli_query($mysqli, $qry_str);
    if (!mysqli_num_rows($last_count)) {
      $inc = 0;
    } else {
      while ($row = mysqli_fetch_array($last_count)) {
        $inc = $post_info["last_count"] - $row["cnt"];
      }
    }
    $rid = mysqli_query($mysqli, "SELECT COUNT(*) AS temp_rid FROM relation");
    if (!mysqli_num_rows($rid)) {
    } else {
      $row = mysqli_fetch_array($rid);
      $rid = $row["temp_rid"];
    }
    
    // Insert tracking information into the tracking table.
    $qry_str = "INSERT INTO tracking VALUES (" . $tid . ",'" . $post_info["last_track"] . "'," . ($track_count + 1) . "," . $inc . "," .  $post_info["last_count"] . ")";
    insert_update($mysqli, $qry_str);
    
    
    // Insert data into the relation table.
    $qry_str = "INSERT INTO relation VALUES (" . $rid . "," . $sid . "," . $post_info["id"] . "," . $tid . ")";
    insert_update($mysqli, $qry_str);
  }

function insert_update($mysqli, $qry_str) {
    $result = mysqli_query($mysqli, $qry_str);
    if (!$result) {
      return 0;
    } else {
      return 1;
    }
 }

?>
